package com.transcendence.appwidget.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.transcendence.appwidget.R;
import com.transcendence.core.utils.log.LogUtils;
import com.transcendence.core.utils.sp.SPUtils;
import com.transcendence.core.utils.text.StringUtils;

import java.util.List;

/**
 * @author joephone
 * @date 2023/9/21
 * @desc
 */
public class EditAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List<String> items;
    private int mCurPos;
    private OnItemClickListener listener;

    private int [] mBgArrays = new int[]{
        R.drawable.bg_app_widget_gradient_default,
        R.drawable.bg_app_widget_gradient_one,
        R.drawable.bg_app_widget_gradient_two,
        R.drawable.bg_app_widget_gradient_three,
        R.drawable.bg_app_widget_gradient_four,
    };

    public EditAdapter(Context context){
        this.mContext = context;
        items = StringUtils.Companion.getStringList(context, R.array.app_index_item);
        mCurPos = SPUtils.getInstance().getInt("AppWidgetBgSelected",0);
        LogUtils.d("当前选中的pos:"+mCurPos);
    }

    public void setSelectPos(int pos) {
        this.mCurPos = pos;
        notifyDataSetChanged();
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new EditViewHolder(LayoutInflater.from(mContext).inflate(R.layout.activity_edit_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        if(viewHolder instanceof EditViewHolder ){
            EditViewHolder holder = (EditViewHolder) viewHolder;
            holder.tvName.setText(items.get(position));
            holder.ivBg.setImageResource(mBgArrays[position]);
            if(mCurPos == position){
                holder.ivSelected.setVisibility(View.VISIBLE);
            } else {
                holder.ivSelected.setVisibility(View.INVISIBLE);
            }
            holder.itemView.setOnClickListener(v -> {
                if(listener!=null){
                    SPUtils.getInstance().save("AppWidgetBgSelected",position);
                    listener.onItemClick(position);
                }
            });
        }
    }



    @Override
    public int getItemCount() {
        return items ==null?0:items.size();
    }

    public class EditViewHolder extends RecyclerView.ViewHolder{

        private TextView tvName;
        private ImageView ivBg;
        private ImageView ivSelected;

        public EditViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            ivBg =itemView.findViewById(R.id.iv_bg);
            ivSelected =itemView.findViewById(R.id.iv_selected);
        }
    }

    public interface OnItemClickListener{
        void onItemClick(int position);
    }

}
